# pygame 第三方模块，写一个小游戏
import pygame
import random
import time

# 初始化Pygame
pygame.init()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 游戏窗口设置
WIDTH = 800
HEIGHT = 600
CELL_SIZE = 20
SPEED = 10

# 初始化窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")

clock = pygame.time.Clock()

# 蛇的初始设置
snake = [(WIDTH // 2, HEIGHT // 2)]
direction = (0, 0)
new_direction = (0, 0)

# 食物设置
food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
        random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)

score = 0

# 游戏主循环
running = True
game_over = False

while running:
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != (0, CELL_SIZE):
                new_direction = (0, -CELL_SIZE)
            elif event.key == pygame.K_DOWN and direction != (0, -CELL_SIZE):
                new_direction = (0, CELL_SIZE)
            elif event.key == pygame.K_LEFT and direction != (CELL_SIZE, 0):
                new_direction = (-CELL_SIZE, 0)
            elif event.key == pygame.K_RIGHT and direction != (-CELL_SIZE, 0):
                new_direction = (CELL_SIZE, 0)
            elif event.key == pygame.K_r and game_over:
                # 重新开始游戏
                snake = [(WIDTH // 2, HEIGHT // 2)]
                direction = (0, 0)
                score = 0
                food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
                        random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)
                game_over = False

    if not game_over:
        direction = new_direction

        # 移动蛇
        if direction != (0, 0):
            new_head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
            snake.insert(0, new_head)

            # 检查是否吃到食物
            if snake[0] == food:
                score += 10
                # 生成新食物
                while True:
                    food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
                            random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)
                    if food not in snake:
                        break
            else:
                snake.pop()

        # 碰撞检测
        head = snake[0]
        # 撞墙检测
        if (head[0] < 0 or head[0] >= WIDTH or
                head[1] < 0 or head[1] >= HEIGHT):
            game_over = True
        # 自撞检测
        if head in snake[1:]:
            game_over = True

    # 绘制界面
    screen.fill(BLACK)

    # 绘制蛇
    for segment in snake:
        pygame.draw.rect(screen, GREEN, (segment[0], segment[1], CELL_SIZE, CELL_SIZE))

    # 绘制食物
    pygame.draw.rect(screen, RED, (food[0], food[1], CELL_SIZE, CELL_SIZE))

    # 显示分数
    font = pygame.font.Font(None, 36)
    text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(text, (10, 10))

    if game_over:
        font = pygame.font.Font(None, 72)
        text = font.render("Game Over!", True, RED)
        screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))
        font = pygame.font.Font(None, 36)
        text = font.render("Press R to restart", True, WHITE)
        screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 + 50))

    pygame.display.flip()
    clock.tick(SPEED)

pygame.quit()